const util = require('../../utils/util.js')
//获取应用实例
const app = getApp()
Page({
  data: {
    src:'',
    width: 250,//宽度
    height: 300,//高度
    max_width:250 ,
    max_height: 300,
    disable_rotate:false,//是否禁用旋转ZSASQSd
    disable_ratio: false,//锁定比例
    limit_move: false,//是否限制移动,
    flag:'',
    index:'',index1:'',index3:'',type:'',min_width:100,min_height:100
  },
  onLoad: function (options) {
  console.log(options);
  
    if(options.flag==4){
      console.log(11);
      
      this.setData({
         flag:options.flag
      })
    }
   
   
    
    if(options.flag==5){
      this.setData({
        flag:options.flag
     })
    }
    if(options.flag==6){
      this.setData({
        flag:options.flag
     })
    }
    if(options.flag==7){
      this.setData({
        flag:options.flag
     })
    }
    if(options.flag==8){
      this.setData({
        flag:options.flag
     })
    }
    if(options.flag==9){
      this.setData({
        flag:options.flag
     })
    }
    if(options.flag==12){
      console.log(999);
      
      this.setData({
        flag:options.flag,
        index1:options.index1
      })
      console.log(this.data.flag);
    }
    if(options.flag==13){
      this.setData({
        flag:13,

      })
    }
    if(options.flag==14){
      this.setData({
        flag:14,
        index3:options.index3
      })
    }
    if(options.flag==15){
      console.log(15);
      
      this.setData({
        flag:15,
        
      })
    }
    if(options.flag==16){
        if(options.type==1){
        this.setData({
          width:208,
          height:291.9,
          max_height:291.9,
          max_width:208,
          min_width:208,
          min_height:291.9

        })
        }
        if(options.type==2){
          this.setData({
            width:291.2,
            height:408.66,
            max_height:408.66,
            max_width:291.2,
            min_width:291.2,
          min_height:408.66
          })
          }
          if(options.type==3){
            this.setData({
              width:183.04,
              height:266.88,
              max_height:266.88,
              max_width:183.04,
              min_width:183.04,
             min_height:266.88
            })
            }
            if(options.type==5){
              this.setData({
                width:291.2,
                height:375.3,
                max_height:375.3,
                max_width:291.2,
                min_width:291.2,
                min_height:375.3
              })
              }
              if(options.type==4){
                this.setData({
                  width:274.56,
                  height:400.32,
                  max_height:400.32,
                  max_width:274.56,
                  min_width:274.56,
                  min_height:400.32
                })
                }
                if(options.type==7){
                  this.setData({
                    width:200,
                    height:300,
                    max_height:300,
                    max_width:200,
                    min_width:200,
                    min_height:300
                  })
                  }
      
      this.setData({
        flag:16,
        type:options.type
        
      })
    }
    if(options.index){
      this.setData({
        flag:2,
        index:options.index
      })
    }




















    this.cropper = this.selectComponent("#image-cropper");
    this.setData({
      src: options.url
    })
  },
  cropperload(e) {
    console.log('cropper加载完成');
  },
  loadimage(e){
    wx.hideLoading();
    console.log('图片');
    this.cropper.imgReset();
  },
  clickcut(e) {
    console.log(e.detail);
    //图片预览
    wx.previewImage({
      current: e.detail.url, // 当前显示图片的http链接
      urls: [e.detail.url] // 需要预览的图片http链接列表
    })
  },
  upload(){
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '加载中',
        })
        const tempFilePaths = res.tempFilePaths[0];
        //重置图片角度、缩放、位置
        that.cropper.imgReset();
        that.setData({
          src: tempFilePaths
        });
      }
    })
  },
  setWidth(e){
    this.setData({
      width: e.detail.value < 10 ? 10 : e.detail.value
    });
    this.setData({
      cut_left: this.cropper.data.cut_left
    });
  },
  setHeight(e){
    this.setData({
      height: e.detail.value < 10 ? 10 : e.detail.value
    });
    this.setData({
      cut_top: this.cropper.data.cut_top
    });
  },
  switchChangeDisableRatio(e){
    //设置宽度之后使剪裁框居中
    this.setData({
      disable_ratio: e.detail.value
    });
  },
  setCutTop(e) {
    this.setData({
      cut_top: e.detail.value
    });
    this.setData({
      cut_top: this.cropper.data.cut_top
    });
  },
  setCutLeft(e) {
    this.setData({
      cut_left: e.detail.value
    });
    this.setData({
      cut_left: this.cropper.data.cut_left
    });
  },
  switchChangeDisableRotate(e) {
    //开启旋转的同时不限制移动
    if (!e.detail.value){
      this.setData({
        limit_move: false,
        disable_rotate: e.detail.value
      });
    }else{
      this.setData({
        disable_rotate: e.detail.value
      });
    }
  },
  switchChangeLimitMove(e) {
    //限制移动的同时锁定旋转
    if (e.detail.value){
      this.setData({
        disable_rotate: true
      });
    }
    this.cropper.setLimitMove(e.detail.value);
  },
  switchChangeDisableWidth(e) {
    this.setData({
      disable_width: e.detail.value
    });
  },
  switchChangeDisableHeight(e) {
    this.setData({
      disable_height: e.detail.value
    });
  },
  submit(){
    this.cropper.getImg((obj)=>{
    /*  app.globalData.imgSrc = obj.url;*/
   /* var pages = getCurrentPages()   
    　　 var currentPage = pages[pages.length - 1] 
    　　var prevPage = pages[pages.length - 2]    
    　　
    　　prevPage.setData({
    　　　backurl:obj.url,
          url:obj.url      })
          wx.navigateBack({
            
            delta:1,
            complete: (res) => {},
          })*/

//
var that=this
var pages = getCurrentPages()   
　　 var currentPage = pages[pages.length - 1] 
　　var prevPage = pages[pages.length - 2]    
　　if(that.data.flag==1){
  prevPage.setData({
    　　　url:obj.url   })
          wx.navigateBack({
            
            delta:1,
            complete: (res) => {},
          })
}else if(that.data.flag==2){
 console.log(88888);
 
 let arr=prevPage.data.arr
 arr[that.data.index]=obj.url
 prevPage.setData({
  　　　arr   })
          wx.navigateBack({
            
            delta:1,
            complete: (res) => {},
          })
}else if(that.data.flag==4){
  console.log(44444);
  
  prevPage.setData({
    　　　url:obj.url  })
    wx.navigateBack({
            
      delta:1,
      complete: (res) => {},
    })
    
}else if(that.data.flag==5){
  console.log(55555);
  
  prevPage.setData({
    　　　url1:obj.url   })
    wx.navigateBack({
            
      delta:1,
      complete: (res) => {},
    })
    
}else if(that.data.flag==6){
  console.log(55555);
  
  prevPage.setData({
    　　　url1:obj.url   })
    wx.navigateBack({
            
      delta:1,
      complete: (res) => {},
    })}
    else if(that.data.flag==7){
      console.log(55555);
      
      prevPage.setData({
        　　　url2:obj.url  })
        wx.navigateBack({
                
          delta:1,
          complete: (res) => {},
        })}
        else if(that.data.flag==8){
          console.log(55555);
          
          prevPage.setData({
            　　　url3:obj.url  })
            wx.navigateBack({
                    
              delta:1,
            
            })}
            else if(that.data.flag==9){
              console.log(55555);
              
              prevPage.setData({
                　　　url4:obj.url   })
                wx.navigateBack({
                        
                  delta:1,
                  complete: (res) => {},
                })}else if(that.data.flag==12){
                  
                  console.log(12)
                  let data=prevPage.data.files
                  console.log(data);
                  
                  let obj1={path:obj.url}
               data.splice(that.data.index1,1,obj1)
                    prevPage.setData({
                        files:data
                    })
                    console.log(data);
                    
                    wx.navigateBack({
                    
                      delta:1,
                    
                    })

                }else if(that.data.flag==13){
                  
                     prevPage.setData({
                       url:obj.url
                     })
                     wx.navigateBack({
                    
                      delta:1,
                    
                    })
                }else if(that.data.flag==14){
                  let data=prevPage.data.files
                  let obj1={path:obj.url}
                  data.splice(that.data.index3,1,obj1)
                    prevPage.setData({
                        files:data,
                        name:obj.url,
                        flag:1
                    })
                  wx.navigateBack({
                 
                   delta:1,
                 
                 })
             }else if(that.data.flag==15){
              
              
                prevPage.setData({
                    url:obj.url
                })
              wx.navigateBack({
             
               delta:1,
             
             })}else if(that.data.flag==16){
              
              
              prevPage.setData({
                  url:obj.url,
                  backurl:obj.url
                  
              })
            wx.navigateBack({
           
             delta:1,
           
           })}
             //

    
      
     
    });
  },
  rotate(){
    //在用户旋转的基础上旋转90°
    this.cropper.setAngle(this.cropper.data.angle+=90);
  },
  top(){
    this.data.top = setInterval(() => {
      this.cropper.setTransform({
        y: -3
      });
    }, 1000 / 60)
  },
  bottom(){
    this.data.bottom = setInterval(() => {
      this.cropper.setTransform({
        y: 3
      });
    }, 1000 / 60)
  },
  left(){
    this.data.left = setInterval(() => {
      this.cropper.setTransform({
        x: -3
      });
    }, 1000 / 60)
  },
  right(){
    this.data.right = setInterval(() => {
      this.cropper.setTransform({
        x: 3
      });
    }, 1000 / 60)
  },
  narrow() {
    this.data.narrow = setInterval(() => {
      this.cropper.setTransform({
        scale: -0.02
      });
    }, 1000 / 60)
  },
  enlarge() {
    this.data.enlarge = setInterval(() => {
      this.cropper.setTransform({
        scale: 0.02
      });
    }, 1000 / 60)
  },
  end(e) {
    clearInterval(this.data[e.currentTarget.dataset.type]);
  },
})
